// --------------------------------------------------------------------
//   name 	: Attackcache
//   desc 	: 
//   version 	: 
//   dev 	: 
// --------------------------------------------------------------------
// DEPENDENCIES
// 
// --------------------------------------------------------------------
// CHANGE HISTORY
// 0.1 	Initial Release
// --------------------------------------------------------------------
	plugins.attackcache = {
		'enhance_game_place' : function() {
	 		if(location.href.match(/&try=confirm/)) { var attackinfo = plugins.attackcache._generate_attackreport();}
	 		if (attackinfo) plugins.attackcache._update_attackcache(attackinfo);
		},
// --------------------------------------------------------------------
// Function : getAttacksbyVillage
// Returns latest attacks for specific village
// --------------------------------------------------------------------
 getAttacksByCoords : function(coord) {
	 var attackinfo = getValue("attacks");
	 var foundattack = false;
	 plugins.attackcache.removeoldattacks();
	 for(var v in attackinfo) {
		 var attack = attackinfo[v];
		 var coords = attack.targetcoords.join('|');
		 // only put in object if it's not already in there or if it's fresher
		 if(coords==coord) {
		 	 //alert("Attack found and being added to cache");
			 foundattack = true;
		 }
	 }
	 log(3, "attackcache found attack");
	 return foundattack;
 },
// --------------------------------------------------------------------
// Function : removeoldattacks
// Returns latest attacks for specific village
// --------------------------------------------------------------------
 removeoldattacks : function() {
	 var attackinfo = getValue("attacks");
	 for(var v in attackinfo) {
		 var attack = attackinfo[v];
		 // only put in object if it's not already in there or if it's fresher
		 if(attack.timefinish < (servertime.getTime()/1000)) {
		 	 //alert("Deleting attack report"+attack.actionid);
			 delete attackinfo[attack.actionid];
		 }
	 }
	 setValue("attacks", attackinfo);
	 log(3, "attackcache deleted attack");
 },

// --------------------------------------------------------------------
// Function : 
// --------------------------------------------------------------------
_generate_attackreport : function() {			
			var form = $("form[action*='action=command']")[0];
			if(!form) return;
			var attackinfo = {
				formtype :  $("input[name='attack']",form)[0].value
				};
			attackinfo.timestamp = servertime.getTime()/1000;
			attackinfo.targetcoords = [ $("input[name='x']",form)[0].value, $("input[name='y']",form)[0].value];
			attackinfo.actionid =  $("input[name='action_id']",form)[0].value;
			attackinfo.troops =  [ $("input[name='spear']",form)[0].value, $("input[name='sword']",form)[0].value,
					$("input[name='axe']",form)[0].value, $("input[name='spy']",form)[0].value,
					$("input[name='light']",form)[0].value, $("input[name='heavy']",form)[0].value,
					$("input[name='ram']",form)[0].value, $("input[name='catapult']",form)[0].value,
					$("input[name='snob']",form)[0].value];
			attackinfo.destination = $('table.main table.vis td:eq(1)')[0].innerHTML;
			attackinfo.player = $('table.main table.vis td:eq(3)')[0].innerHTML;
			attackinfo.duration = $('table.main table.vis td:eq(5)')[0].innerHTML;
			var timetofinish = attackinfo.duration.split(":");
			//alert("Time to Finish"+timetofinish[0]+","+timetofinish[1]+","+timetofinish[2]);
			attackinfo.timefinish = attackinfo.timestamp+timetofinish[0]*3600+timetofinish[1]*60+timetofinish[2]*1;
			attackinfo.arrival = $('table.main table.vis td:eq(7)')[0].innerHTML;
			attackinfo.Morale = $('table.main table.vis td:eq(9)')[0].innerHTML;
		return attackinfo;	
		// TODO - add onsubmit that adds to attackcache
		},
// --------------------------------------------------------------------
// Function : 
// --------------------------------------------------------------------
 _update_attackcache : function(attackinfo) {
	 var attacks = getValue("attacks");
	 if(!attacks) attacks = {};
	 attacks[attackinfo.actionid] = attackinfo;
	 setValue("attacks", attacks);
	 log(3, "attackcache updated attack="+uneval(attackinfo));
 },

// --------------------------------------------------------------------
// Information array 
// --------------------------------------------------------------------
   'info' : {
   'mandatory' : false,
   'name' : "attackCache",
   'desc' : "Adds an attackcache option",
   'version' : "0.1",
   'dev' : "ocdcsv"
  }

	};
